<?php
require_once 'Pusher.php';

class ajax extends baseController {    
	public function __construct() {
		parent::__construct ();
	}
    
    public function postcomment() {
        if (!empty($_POST['entryid']) && !empty($_POST['content'])) {
            $data['entryid'] = $_POST['entryid'];
            $account = $this->session->userdata('account');
            $data['accountid'] = $account['id'];
            $data['startdate'] = date('Y-m-d H:i:s');
            $data['content'] = $_POST['content'];//mysql_real_escape_string($_POST['content']);
            $this->commentModel->insert($data);
            //echo "<blockquote class='pull-right' style='text-align: right; width:100%'><h6>".$data['content']."</h6><small>Posted by ".$account['username']." on ". date('d-m-Y H:i:s', strtotime($data['startdate']))."</small></blockquote>";
            $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
            $data = array('name' => $account['username'], 'comment' => $_POST['content'], 'date' => date('d-m-Y H:i:s', strtotime($data['startdate'])));
            $pusher->trigger('entry'.$_POST['entryid'], 'comment', $data);
        }
    }
    
    public function postcommentquestion() {
        if (!empty($_POST['questionid']) && !empty($_POST['content'])) {
            $data['questionid'] = $_POST['questionid'];
            $account = $this->session->userdata('account');
            $data['accountid'] = $account['id'];
            $data['startdate'] = date('Y-m-d H:i:s');
            $data['content'] = $_POST['content'];//mysql_real_escape_string($_POST['content']);
            $this->commentModel->insert($data);
            //echo "<blockquote class='pull-right' style='text-align: right; width:100%'><h6>".$data['content']."</h6><small>Posted by ".$account['username']." on ". date('d-m-Y H:i:s', strtotime($data['startdate']))."</small></blockquote>";
            $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
            $data = array('name' => $account['username'], 'comment' => $_POST['content'], 'date' => date('d-m-Y H:i:s', strtotime($data['startdate'])));
            $pusher->trigger('comments'.$_POST['questionid'], 'comment', $data);
        }
    }
    
    public function postquestion() {
        if (!empty($_POST['title']) && !empty($_POST['question'])) {
            $account = $this->session->userdata('account');
            $data['accountid'] = $account['id'];
            $data['answerid'] = 0;
            $data['startdate'] = date('Y-m-d H:i:s');
            $data['title'] = $_POST['title'];
            $data['question'] = $_POST['question'];//mysql_real_escape_string($_POST['content']);
            $data['views'] = 1;
            $data['statuss'] = 1;
            $slug = mb_strtolower(url_title(removesign($data['title'])));
            if($this->questionModel->check_exist('slug', $slug)) {
                $slug = $slug.'-'.random_string('alnum', 4);
            }
            $data['slug'] = $slug;
            $this->questionModel->insert($data);
            $current = $this->questionModel->readLast();
            $id = $current['id'];
            
            echo "<div class='bs-callout bs-callout-danger'><quote>".$data['title']."<cite>".$data['question']."<br /><p style='text-align: right;'>
            Ðăng bởi: ".$account['username']." vào lúc ".$data['startdate']."</p></cite></quote></div>";
        }
    }
    
    public function getSubs() {
        if (!empty($_GET['id'])) {
            $selected = $_GET['selected'];
            $subs = $this->categoryModel->get_order_where(null, 'number', 'asc', array('parentid' => $_GET['id']));
            foreach($subs as $k=>$v) {
                if($v['id'] == $selected) {
                    echo '<option value='.$v['id'].' selected >'.$v['categoryname'].'</option>';
                } else {
                    echo '<option value='.$v['id'].'>'.$v['categoryname'].'</option>';
                }
            }
        }
    }
}
?>